package com.rupertjones.globalcron.web;

import com.rupertjones.globalcron.domain.JobExecutionLog;
import com.rupertjones.globalcron.web.model.DisplayableJob;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.controller.LifecycleStage;

/**
 * <p>&copy Rupert Jones 2011</p>
 *
 * @author rup
 */
public class JobLogInfoAction extends AbstractAction {

    private DisplayableJob job;

    private JobExecutionLog log;

    @Before(stages = LifecycleStage.BindingAndValidation)
    public void loadAndBind() {
        String paramJobId = getContext().getRequest().getParameter("job.id");
        int jobId = Integer.parseInt(paramJobId);
        String paramLogId = getContext().getRequest().getParameter("log.id");
        int logId = Integer.parseInt(paramLogId);

        job = getJobService().getDisplayableJob(jobId);
        log = job.getLog(logId);
    }

    public boolean isRunningJob() {
        return getJobService().hasEnabledJob(job.getId());
    }

    public DisplayableJob getJob() {
        return job;
    }

    public JobExecutionLog getLog() {
        return log;
    }

    public String getOutput() {
        return log.getOutput().replace("/\n", "<br/>");
    }

    @DefaultHandler
    public Resolution view() {
        return new ForwardResolution("/WEB-INF/jsp/job_log_info.jsp");
    }
}
